package com.ls.lsqc.modules.sys.utils;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.h2.mvstore.DataUtils;

import com.ls.lsqc.common.utils.DateUtils;
import com.ls.lsqc.common.utils.ResourcesUtil;
import com.ls.lsqc.common.utils.SpringContextHolder;
import com.ls.lsqc.common.utils.StringUtils;
import com.ls.lsqc.modules.sys.entity.Dict;
import com.ls.lsqc.modules.sys.entity.Sequence;
import com.ls.lsqc.modules.sys.service.SequenceService;

public class SequenceUtil {
    
    private static SequenceService sequenceService = SpringContextHolder.getBean(SequenceService.class);
    
    /**
     * 店铺编号
     * @return
     */
    public static Sequence shopCode(String shortCity){
        
        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig("sequence_shop"));
        
        NumberFormat formatter = NumberFormat.getNumberInstance();     
        formatter.setMinimumIntegerDigits(2);     
        formatter.setGroupingUsed(false);     
        String s = formatter.format(sequence.getNumber());    
        
        sequence.setReturnCode(shortCity+s);
        return sequence;
    }
    /**
     * 订单编号
     * @return
     */
    public static Sequence orderCode(){
        
        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig("sequence_order"));
        
        NumberFormat formatter = NumberFormat.getNumberInstance();     
        formatter.setMinimumIntegerDigits(4);     
        formatter.setGroupingUsed(false);     
        String s = formatter.format(sequence.getNumber());    
        
        String cityCode=UserUtils.getUser().getShop().getShopCode();
        
        sequence.setReturnCode(cityCode+ DateUtils.formatDate(new Date(), "yyMM")+s);
        
        return sequence;
    }
    /**
     * 物料条形码
     * @param sequence
     */
    public static Sequence materialbarcode(){
        
        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig("sequence_materialbarcode"));
        NumberFormat formatter = NumberFormat.getNumberInstance();     
        formatter.setMinimumIntegerDigits(6);     
        formatter.setGroupingUsed(false);     
        String s = formatter.format(sequence.getNumber());    
        
        sequence.setReturnCode(DateUtils.formatDate(new Date(), "yyMMdd")+s);
        
        return sequence;
    }
    
    /**
     * 仓库条形码
     * @param sequence
     */
    public static Sequence warehousebarcode(){
        
        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig("sequence_warehouse"));
        NumberFormat formatter = NumberFormat.getNumberInstance();     
        formatter.setMinimumIntegerDigits(6);     
        formatter.setGroupingUsed(false);     
        String s = formatter.format(sequence.getNumber());    
        
        sequence.setReturnCode(DateUtils.formatDate(new Date(), "yyMMdd")+s);
        
        return sequence;
    }
    /**
     * 货架条形码
     * @param sequence
     */
    public static Sequence shelvesbarcode(){
        
        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig("sequence_shelves"));
        NumberFormat formatter = NumberFormat.getNumberInstance();     
        formatter.setMinimumIntegerDigits(6);     
        formatter.setGroupingUsed(false);     
        String s = formatter.format(sequence.getNumber());    
        
        sequence.setReturnCode("9"+DateUtils.formatDate(new Date(), "yyMMdd")+s);
        
        return sequence;
    }
    
    /**
     * 款号
     * @param season
     * @param category
     * @return
     */
    public static Sequence styleNo(String season,String style ){
        
        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig("style_no"));
//        int seasonInt=0;
//        //季节
//        if(StringUtils.isNotBlank(season)){
//            Dict seasonDict=DictUtils.getDictEntity(season);
//            seasonInt=seasonDict.getItemSort();
//        }
//        //款式类型
//        Dict styleDict=DictUtils.getDictEntity(style);
//        NumberFormat formatterStyle = NumberFormat.getNumberInstance();     
//        formatterStyle.setMinimumIntegerDigits(6);     
//        formatterStyle.setGroupingUsed(false);     
//        String styleStr = formatterStyle.format(styleDict.getItemSort());    
        
        //连翻
        NumberFormat formatter = NumberFormat.getNumberInstance();     
        formatter.setMinimumIntegerDigits(3);     
        formatter.setGroupingUsed(false);     
        String s = formatter.format(sequence.getNumber());   
        
        //String returnCode=DateUtils.formatDate(new Date(), "yy")+seasonInt+"0"+styleStr+s;
        
        sequence.setReturnCode(s);
        
        return sequence;
    }
    
    /**
     * 客户，供货商，加工厂编号
     * @param pCode   
     * @param String
     * @param min
     * @return
     */
    public static Sequence contractCode(String pCode,String pConfig,int min){
        String nowDate = "."+DateUtils.getDate("yyyyMMdd")+".";
        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig(pConfig));
        
        NumberFormat formatter = NumberFormat.getNumberInstance();     
        formatter.setMinimumIntegerDigits(min);     
        formatter.setGroupingUsed(false);     
        String s = formatter.format(sequence.getNumber());
        sequence.setReturnCode(pCode + nowDate + s);
        return sequence;
    }
    
    /**
     * 客户，供货商，加工厂编号
     * @param pCode   
     * @param String
     * @param min
     * @return
     */
    public static Sequence ognizationCode(String pCode,String pConfig,int min){
        
        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig(pConfig));
        
        NumberFormat formatter = NumberFormat.getNumberInstance();     
        formatter.setMinimumIntegerDigits(min);     
        formatter.setGroupingUsed(false);     
        String s = formatter.format(sequence.getNumber());
        sequence.setReturnCode(pCode+s);
        return sequence;
    }
    /**
     * 定制订单条形码
     * @param pCode   
     * @param String
     * @param min
     * @return
     */
    public static Sequence orderStyleCode(String pCode,String pConfig,int min){
        
        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig(pConfig));
        
        NumberFormat formatter = NumberFormat.getNumberInstance();     
        formatter.setMinimumIntegerDigits(min);     
        formatter.setGroupingUsed(false);     
        String s = formatter.format(sequence.getNumber());
        sequence.setReturnCode(pCode+s);
        return sequence;
    }
    
    /**
     * 样品需求单号
     * @param pCode   
     * @param String
     * @param min
     * @return
     */
    public static Sequence offerCode(String pCode,String pConfig,int min,String styleCode){
        
        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig(pConfig));
        
        NumberFormat formatter = NumberFormat.getNumberInstance();     
        formatter.setMinimumIntegerDigits(min);     
        formatter.setGroupingUsed(false);     
        String s = formatter.format(sequence.getNumber());    
        StringBuilder conCode = new StringBuilder();
        conCode.append(pCode+s);
        conCode.append(styleCode);
        sequence.setReturnCode(conCode.toString());
        return sequence;
    }
    /**
     * 入库申请条形码
     * @param sequence
     */
    public static Sequence inventoryRequestbarcode(){
        
        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig("sequence_inventoryRequestbarcode"));
        NumberFormat formatter = NumberFormat.getNumberInstance();     
        formatter.setMinimumIntegerDigits(6);     
        formatter.setGroupingUsed(false);     
        String s = formatter.format(sequence.getNumber());    
        
        sequence.setReturnCode(DateUtils.formatDate(new Date(), "yyMMdd")+s);
        
        return sequence;
    }
    
    /**
     * 入库申请号
     * @param sequence
     */
    public static Sequence inventoryRequest(){
        
        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig("sequence_inventoryRequest"));
        NumberFormat formatter = NumberFormat.getNumberInstance();     
        formatter.setMinimumIntegerDigits(6);     
        formatter.setGroupingUsed(false);     
        String s = formatter.format(sequence.getNumber());    
        
        sequence.setReturnCode(DateUtils.formatDate(new Date(), "yyyyMMdd")+s);
        
        return sequence;
    }
    
    /**
     * 入库申请号
     * @param sequence
     */
    public static Sequence inventoryRequestEx(){
        
        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig("sequence_inventoryRequestEx"));
        NumberFormat formatter = NumberFormat.getNumberInstance();     
        formatter.setMinimumIntegerDigits(6);     
        formatter.setGroupingUsed(false);     
        String s = formatter.format(sequence.getNumber());    
        
        sequence.setReturnCode(DateUtils.formatDate(new Date(), "yyyyMMdd")+s);
        
        return sequence;
    }
    
    /**
     * 成衣入库申请条形码
     * @param sequence
     */
    public static Sequence slopworkRequestbarcode(){
        
        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig("sequence_slopworkRequestbarcode"));
        NumberFormat formatter = NumberFormat.getNumberInstance();     
        formatter.setMinimumIntegerDigits(6);     
        formatter.setGroupingUsed(false);     
        String s = formatter.format(sequence.getNumber());    
        
        sequence.setReturnCode(DateUtils.formatDate(new Date(), "yyMMdd")+s);
        
        return sequence;
    }

    
    /**
     * 成衣出库申请条形码
     * @param sequence
     */
    public static Sequence outslopworkRequestbarcode(){
        
        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig("sequence_outslopworkRequestbarcode"));
        NumberFormat formatter = NumberFormat.getNumberInstance();     
        formatter.setMinimumIntegerDigits(6);     
        formatter.setGroupingUsed(false);     
        String s = formatter.format(sequence.getNumber());    
        
        sequence.setReturnCode(DateUtils.formatDate(new Date(), "yyMMdd")+s);
        
        return sequence;
    }


    /**
     * 内部款号Excel用
     * @return
     */
    public static Sequence styleNoExcel(){

        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig("style_no"));
        //连翻
        String[] letters = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z".split(",");
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(6);
        formatter.setGroupingUsed(false);
        String s = formatter.format(sequence.getNumber());
        String sNum = s.substring(s.length()-4, s.length());
        int sIndex = Integer.parseInt(s.substring(0, 2));
        if ("9999".equals(sNum)&&sIndex==25) {
        	sequenceService.updateNumber(ResourcesUtil.getConfig("style_no"));
		}else if ("9999".equals(sNum)&&sIndex!=25) {
			sequenceService.getSequence(ResourcesUtil.getConfig("style_no"));
		}
        sequence.setReturnCode(letters[sIndex]+"-"+sNum);
        return sequence;
    }
    
    /**
     * 内部款号Excel用
     * @return
     * @throws ParseException 
     */
    public static Sequence styleNoHandle() throws ParseException{

        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig("style_no"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(DateUtils.getDate("yyyy-MM-dd"));
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		int calendarNum = calendar.get(Calendar.WEEK_OF_MONTH);
        //连翻
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(6);
        formatter.setGroupingUsed(false);
        String s = formatter.format(sequence.getNumber());
        String sNum = s.substring(s.length()-4, s.length());
        int sIndex = Integer.parseInt(s.substring(0, 2));
        if ("9999".equals(sNum)&&sIndex==25) {
        	sequenceService.updateNumber(ResourcesUtil.getConfig("style_no"));
		}else if ("9999".equals(sNum)&&sIndex!=25) {
			sequenceService.getSequence(ResourcesUtil.getConfig("style_no"));
		}
        sequence.setReturnCode(Integer.parseInt(DateUtils.getDate("MM"))+"M"+calendarNum+"-"+sNum);
        return sequence;
    }
    
    /**
     * 内部款号Excel用
     * @return
     * @throws ParseException 
     */
    public static Sequence styleNoCustomH(String customName) throws ParseException{
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(DateUtils.getDate("yyyy-MM-dd"));
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		int calendarNum = calendar.get(Calendar.WEEK_OF_MONTH);
    	String customStr = customName+"."+Integer.parseInt(DateUtils.getDate("MM"))+"M"+calendarNum;
        if (sequenceService.getByCode(customStr)==null) {
        	sequenceService.insertSequence(customStr);
		}
        Sequence sequence=sequenceService.getSequence(customStr);
        //连翻
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(4);
        formatter.setGroupingUsed(false);
        String s = formatter.format(sequence.getNumber());
        if ("9999".equals(s)) {
        	sequenceService.updateNumber(customStr);
		}
        sequence.setReturnCode(customStr+"-"+s);
        return sequence;
    }
    /**
     * 内部款号Excel用
     * @return
     * @throws ParseException 
     */
    public static Sequence styleNoCustom(String customName) throws ParseException{
    	String[] letters = "A,B,C,D,E".split(",");
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(DateUtils.getDate("yyyy-MM-dd"));
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		int calendarNum = calendar.get(Calendar.WEEK_OF_MONTH);
    	String customStr = customName+"."+DateUtils.getDate("MM")+letters[calendarNum-1];
        if (sequenceService.getByCode(customStr)==null) {
        	sequenceService.insertSequence(customStr);
		}
        Sequence sequence=sequenceService.getSequence(customStr);
        //连翻
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(4);
        formatter.setGroupingUsed(false);
        String s = formatter.format(sequence.getNumber());
        if ("9999".equals(s)) {
        	sequenceService.updateNumber(customStr);
		}
        sequence.setReturnCode(customStr+"-"+s);
        return sequence;
    }
    
    /**
     * 生产号生成规则
     * @return
     * @throws ParseException 
     */
    public static Sequence styleNoCloud(String customName,String makeType) throws ParseException{
    	return sequenceService.styleNoCloud(customName,makeType);
    }

    /**
     * 获取船期
     * @return
     * @throws ParseException 
     */
    public static Date getShipTime(String makeType) throws ParseException{
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Sequence sequence=sequenceService.getByCode(makeType);
        String dateStr = sequence.getNumber().toString();
        dateStr = dateStr.substring(0, 4)+"-"+dateStr.substring(4, 6)+"-"+dateStr.substring(6, 8);
        Date date = sdf.parse(dateStr);
        return date;
    }
    
    /**
     * 获取生产期号
     * @return
     * @throws ParseException 
     */
    public static String getKeyWord(String makeType) throws ParseException{
        Sequence sequence=sequenceService.getKeyWord(makeType);
        return sequence.getKeyWord();
    }
    
    /**
     * 共通code
     * @param pCode   
     * @param String
     * @param min
     * @return
     */
    public static Sequence commonCode(String pCode,String pConfig,int min){
        
        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig(pConfig));
        
        NumberFormat formatter = NumberFormat.getNumberInstance();     
        formatter.setMinimumIntegerDigits(min);     
        formatter.setGroupingUsed(false);     
        String s = formatter.format(sequence.getNumber());
        sequence.setReturnCode(pCode+s);
        return sequence;
    }
    /**
     * 成衣入库申请号
     * @param sequence
     */
    public static Sequence getStyleRequest(String header ,String styleNo,int min){

        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig("sequence_styleRequestIn"));

        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(min);
        formatter.setGroupingUsed(false);
        String s = formatter.format(sequence.getNumber());
        sequence.setReturnCode(header+styleNo+s);
        return sequence;
    }
    
    /**
     * 财务业务结算号
     * @param sequence
     * @throws ParseException 
     */
    public static Sequence financeCode() throws ParseException{
        
        return sequenceService.financeCode();
    }
    
    /**
     * 会计凭证编码
     * @param pCode   
     * @param String
     * @param min
     * @return
     */
    public static Sequence voucherCode(String pCode,String pConfig,int min){
        
        Sequence sequence=sequenceService.getSequence(ResourcesUtil.getConfig(pConfig));
        
        NumberFormat formatter = NumberFormat.getNumberInstance();     
        formatter.setMinimumIntegerDigits(min);     
        formatter.setGroupingUsed(false);     
        String s = formatter.format(sequence.getNumber());
        sequence.setReturnCode(pCode+"."+DateUtils.getDate("yyyyMM")+s);
        return sequence;
    }
    
}
